/**
 * Created by digidite on 2017/12/13.
 */
//当前光标所在 0 内容上  1 上下页  2 左边
var focus = 0 ;
//当前页
var currPage = 1;
//当前对象
var currContent = 1;
//内容对象
var content = null;
//内容记录数
var total =0;
//上一页下一页
var result = null;
//左边 当前选的是哪个？
var select =0;

var currData = null;

var type = 2 ;//GetQueryString("type");

//获取保险列表
var productJson = null;

window.onload = function () {
    var id = GetQueryString("id");
    var name = GetQueryString("name");
    if(id!=null){
        $("title").innerHTML=name+"保险";
        getProductList(id);
    }
};


function showProductList() {
    if(currData.length>0) {
        document.getElementById("list").innerHTML = '';
        total = Math.ceil(currData.length / 6);//总页数
        var show = 6;
        if (currPage == total) {
            show = currData.length - (currPage - 1) * 6;
        } else {
            show = 6;
        }
        var startIndex = (currPage - 1) * 6;
        for (var i = 0; i < show; i++) {
            createProduct(currData[startIndex], i + 1);
            startIndex = startIndex + 1;
        }
        init();
        if (focus == 0) {
            addBorder(currContent);
        }
        $("curr").innerHTML = currPage;
        $("total").innerHTML = total;
    }
}

function createProduct(obj,n) {
    if(obj!=null){
        var parNode = document.getElementById("list");
        parNode.innerHTML+='<div class="content-'+n+'" id="content-'+n+'"> ' +
            '<div class="name">'+obj.name+'</div> ' +
            '<div class="insurance-calculate">' +
            '<span class="insurance-money">'+obj.money+'</span>元起</div>' +
            ' <div class="insurance-sales">销量 <span>'+obj.sell+'</span>份</div>' +
            ' </div>';
    }

}

var all_div = null;
function init() {
    if (total > 1) {
        var result1 = $("result-1");
        var result2 = $("result-2");
        if (currPage == 1) {//第一页
            result1.style.display = 'block';
            result2.style.display = 'none';
            result1.innerHTML = '下一页';
        } else if (currPage < total) {//中间页
            result1.style.display = 'block';
            result2.style.display = 'block';
            result1.innerHTML = '上一页';
        } else if (currPage == total) {
            result1.style.display = 'block';
            result2.style.display = 'none';
            result1.innerHTML = '上一页';
        }
    } else if (total == 1) {
        $("result-1").style.display = 'none';
        $("result-2").style.display = 'none';
    }
    content = $("content-1");
    all_div = $("list").getElementsByTagName('div');
}
function addBorder(num){
    if(num>0){
        document.getElementById("resultFocus").style.display='none';
        document.getElementById("selectFocus").style.display='none';
        var obj = document.getElementById("contentFocus");
        obj.style.display='block';
        if(num%2==0){//偶数
            obj.style.left=762+"px";
            obj.style.top=(98+(num/2-1)*164)+"px";
        }else if(num%2==1) {
            obj.style.left=304+"px";
            obj.style.top=(98+((num-1)/2)*164)+"px";
        }
    }else if(num==-1){
        currContent=1;
        document.getElementById("contentFocus").style.display='none';
    }
}

function addResultFocus(num) {
    document.getElementById("contentFocus").style.display='none';
    var obj = document.getElementById("resultFocus");
    obj.style.display='block';
    obj.style.left=(305+ num*146)+ "px";
}

function addSelectFocus(num) {
    if(num>0){
        document.getElementById("contentFocus").style.display='none';
        var obj = document.getElementById("selectFocus");
        obj.style.display='block';
        if(type==1){
            if(num<3){
                obj.style.top=(98+ (num-1)*50)+ "px";
            }if(num>=3){
                num=num-2;
                obj.style.top=(298+ (num-1)*50)+ "px";
            }
        }else if(type==2){
            if(num<=3){
                obj.style.top=(98+ (num-1)*50)+ "px";
            }
        }
    }

}